using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class AskForBreakThroughDialog : Form
    {
        private List<MapInfo.CountryID> listOfPotentialSpecialActions;
        private GameController.breakThroughSelection selection;
        private GameController.playerType playerBreakingThrough;
        private IGameStateInfoProvider infoProvider;
        public AskForBreakThroughDialog(GameController.playerType playerBreakingThrough,bool canBreakThroughCombat, IGameStateInfoProvider infoProvider)
        {
            InitializeComponent();
            selection.breakthroughPhase = GameController.breakThroughPhaseType.Pass;
            selection.specialActionsSpent = new List<MapInfo.CountryID>();
            this.ContinueToFinalSupplyRadioButton.Checked = true;
            if (!canBreakThroughCombat)
                this.BreakThroughCombatRadioButton.Enabled = false;
            listOfPotentialSpecialActions = new List<MapInfo.CountryID>();
            this.playerBreakingThrough = playerBreakingThrough;
            this.infoProvider = infoProvider;
            SetupSpecialActionSelection();
        }

        private void SetupSpecialActionSelection()
        {
            listOfPotentialSpecialActions.Clear();
            if(playerBreakingThrough == GameController.playerType.AlliedPlayer)
            {
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.France) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.France);
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.GreatBritain) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.GreatBritain);
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.USSR) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.USSR);
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.USA) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.USA);
            }
            else
            {
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.Germany) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.Germany);
                if (infoProvider.GetNumSpecialActions(MapInfo.CountryID.Italy) > 0)
                    listOfPotentialSpecialActions.Add(MapInfo.CountryID.Italy);
            }

            this.SuspendLayout();
            int tabStop = 10;
            int xPos = this.ContinueToFinalSupplyRadioButton.Location.X;
            int yPos = this.ContinueButton.Location.Y + this.ContinueButton.Size.Height + 5;
            foreach (MapInfo.CountryID country in listOfPotentialSpecialActions)
            {
                PictureBox pictureBoxToAdd = new PictureBox();
                ((System.ComponentModel.ISupportInitialize)(pictureBoxToAdd)).BeginInit();
                pictureBoxToAdd.Location = new System.Drawing.Point(xPos, yPos);
                pictureBoxToAdd.Name = Enum.GetName(country.GetType(),country);
                pictureBoxToAdd.Size = new System.Drawing.Size(65, 65);
                pictureBoxToAdd.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                pictureBoxToAdd.TabIndex = tabStop;
                pictureBoxToAdd.TabStop = false;
                pictureBoxToAdd.Paint += new PaintEventHandler(pictureBoxToAdd_Paint);
                pictureBoxToAdd.Click += new EventHandler(pictureBoxToAdd_Click);
                ((System.ComponentModel.ISupportInitialize)(pictureBoxToAdd)).EndInit();
                yPos += 70;
                tabStop++;
                this.Controls.Add(pictureBoxToAdd);
            }

            this.Size = new Size(this.Size.Width, yPos);
            this.ContinueButton.Top =  this.Size.Height - this.ContinueButton.Height - 10;
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        void pictureBoxToAdd_Click(object sender, EventArgs e)
        {
            MapInfo.CountryID countryToToggle = (MapInfo.CountryID)Enum.Parse(typeof(MapInfo.CountryID), ((PictureBox)sender).Name);
            if (selection.specialActionsSpent.Contains(countryToToggle))
                selection.specialActionsSpent.Remove(countryToToggle);
            else
                selection.specialActionsSpent.Add(countryToToggle);
            ((PictureBox)sender).Invalidate();
        }

        void pictureBoxToAdd_Paint(object sender, PaintEventArgs e)
        {
            MapInfo.CountryID countryToPaint = (MapInfo.CountryID)Enum.Parse(typeof(MapInfo.CountryID), ((PictureBox)sender).Name);

            if(selection.specialActionsSpent.Contains(countryToPaint))
                PlayingPieceGraphics.PaintSpecialActions(e.Graphics, countryToPaint, new Point(((PictureBox)sender).Width / 2, ((PictureBox)sender).Height / 2));
            else
                PlayingPieceGraphics.PaintTransparentSpecialActions(e.Graphics, countryToPaint, new Point(((PictureBox)sender).Width / 2, ((PictureBox)sender).Height / 2));
        }

        public GameController.breakThroughSelection GetSelection()
        {
            if (ContinueToFinalSupplyRadioButton.Checked)
                this.selection.breakthroughPhase = GameController.breakThroughPhaseType.Pass;
            else if (breakthroughOpsRadioButton.Checked)
                this.selection.breakthroughPhase = GameController.breakThroughPhaseType.BreakthroughOpsMove;
            else if (BreakthroughStratRadioButton.Checked)
                this.selection.breakthroughPhase = GameController.breakThroughPhaseType.BreakthroughStrat;
            else if (BreakThroughCombatRadioButton.Checked)
                this.selection.breakthroughPhase = GameController.breakThroughPhaseType.BreakthroughCombat;
            return this.selection;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (selection.specialActionsSpent.Count > 0 || this.ContinueToFinalSupplyRadioButton.Checked)
                this.Close();
            else
                MessageBox.Show("You must spend at least 1 special action");
        }
    }
}